package org.csu.backofficemanagersys.config;

import org.csu.backofficemanagersys.interceptor.Base64Interceptor;
import org.csu.backofficemanagersys.interceptor.LogInterceptor;
import org.csu.backofficemanagersys.interceptor.TokenInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
@MapperScan("org.csu.backofficemanagersys.mapper")
public class WebMvcConfig implements WebMvcConfigurer {



    @Autowired
    private LogInterceptor logInterceptor;
    @Autowired
    private Base64Interceptor base64Interceptor;
    @Autowired
    private TokenInterceptor tokenInterceptor;


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路由
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOriginPatterns("*")
                // 是否允许携带cookie参数
                .allowCredentials(true)
                // 设置允许的方法
                .allowedMethods("*")
                // 跨域允许时间
                .maxAge(3600);
    }

    /**
     * 拦截器配置
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {

        //注册Interceptor拦截器
//        InterceptorRegistration registration = registry.addInterceptor(new IsLoginInterceptor());
//        registration.addPathPatterns("/**"); //所有路径都被拦截
//        registration.excludePathPatterns( //添加不拦截路径
//                "/",
//                "/loginForm",   //登录页面
//                "/login",       //登录请求
//                "/**/*.html",   //html静态资源
//                "/**/*.js",     //js静态资源
//                "/**/*.css"     //css静态资源
//        );

        //直接在addInterceptor()中new一个的话，spring不会管理这个类，也不会自动注入。所以就报错了
        registry.addInterceptor(logInterceptor).addPathPatterns("/**").excludePathPatterns("/**/*.html",   //html静态资源
                                                                                                "/**/*.js",     //js静态资源
                                                                                                "/**/*.css");
/*
        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**")
                .excludePathPatterns(
                        "/mypetstore/login",
                        "/mypetstore/loginphone",
                        "/mypetstore/code/{phone}",
                        "/** /*.html",   //html静态资源
                        "/** /*.js",     //js静态资源
                        "/** /*.css");

*/
    }
}
